package jdbc;

import java.sql.*;
import java.util.Scanner;

public class JDBCReg {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        try ( Connection connection = DBUtil.getConnection();){
            System.out.println("请输入用户名：");
            String username=scanner.nextLine();
            System.out.println("密码：");
            String password=scanner.nextLine();
            System.out.println("请输入匿名：");
            String nickname=scanner.nextLine();
            System.out.println("请输入年龄：");
            int age=scanner.nextInt();
            String str2="select username,password,nickname,age from userinfo where username=?";
            PreparedStatement st2 = connection.prepareStatement(str2);
            st2.setString(1,username);
            ResultSet rt3 = st2.executeQuery();
            if(rt3.next()){
                System.out.println("注册失败");
            }else {
                String str="insert into user values(?,?,?,?)";
                PreparedStatement st = connection.prepareStatement(str);
                st.setString(1,username);
                st.setString(2,password);
                st.setString(3,nickname);
                st.setInt(4,age);
                int i = st.executeUpdate();
                if(i>0){
                    System.out.println("注册成功");
                }
                System.out.println("注册失败");
            }


            //PreparedStatement ps = connection.prepareStatement();


        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }
}
